/**
 * 分页
 */
export class Pagination {

    /**
     * 获取临近的页码
     *
     * @param current 当前页码
     * @param count 总页码
     * @returns {any}
     */
    getPage(current : number, count : number) {
        let result : any = {};
        result.current = current;
        result.count = count;
        // 计算分页显示区间
        const step = 3;
        let start : number,
             end : number;
        start = current - (step - 1) /2;
        if (start < 1) {
            start = 1;
            end = (step > count) ? count :step;
        } else {
            end = start + step -1;
            if (end > count) {
                let offset = start - (end - count);
                start = (offset < 1) ? 1 : offset;
                end = count;
            }
        }
        result.page = [];
        for (let i = start; i <= end; i++) {
            result.page.push(i);
        }
        return result;
    }

    /**
     * 上页
     *
     * @param current
     * @returns {number}
     */
    public previousPage(current : number) {
        const page = (current > 1) ? current - 1 : 1;
        return page;
    }

    /**
     * 下页
     *
     * @param current
     * @param count
     * @returns {number}
     */
    public nextPage(current : number, count : number) {
        const page = (current < count) ? current + 1 : count;
        return page;
    }

}